package com.heima.behavior.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.UnlikesBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author changjiang
 * @since 2022-03-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Autowired
    private IApBehaviorEntryService entryService;




    @Override
    public ResponseResult unlikesBehavior(UnlikesBehaviorDto dto) {
        // 查询行为实体
        EntryDto entryDto = new EntryDto();
        entryDto.setUserId(dto.getUserId());
        entryDto.setEquipmentId(dto.getEquipmentId());
        ApBehaviorEntry entry =entryService.getEntry(entryDto);
        //构建ApFollowBehavior对象
        ApUnlikesBehavior unlikesBehavior = new ApUnlikesBehavior();
        if (entry != null) {
            unlikesBehavior.setEntryId(entry.getId());
        }
        unlikesBehavior.setArticleId(dto.getArticleId());
        unlikesBehavior.setType(dto.getType());
        unlikesBehavior.setCreatedTime(new Date());
        // 判断是否已经有不喜欢的记录,有的话不再进行保存
        LambdaQueryWrapper<ApUnlikesBehavior> query = new LambdaQueryWrapper<>();
        query.eq(ApUnlikesBehavior::getEntryId, entry.getId());
        query.eq(ApUnlikesBehavior::getArticleId, dto.getArticleId());
        ApUnlikesBehavior one = this.getOne(query);
        if (one == null) {
            this.save(unlikesBehavior);
        } else {
            if (dto.getType() != one.getType()) {
                one.setType(dto.getType());
                this.updateById(one);
            }
        }
        return ResponseResult.okResult();
    }




}
